package com.itheima.service.impl;

import com.itheima.common.utils.DateUtils;
import com.itheima.common.utils.SMSUtils;
import com.itheima.dto.OrderDTO;
import com.itheima.mapper.OrderMapper;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 预约服务接口实现类
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    /**
     * 预约Mapper
     */
    @Autowired
    private OrderMapper orderMapper;
    /**
     * RedisTemplate
     */
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 根据预约id查询预约信息
     *
     * @param id
     * @return
     */
    @Override
    public Object findById(Integer id) {
        return orderMapper.findById(id);
    }

    /**
     * 提交预约信息
     *
     * @param orderDTO
     */
    @Override
    public void submitOrder(OrderDTO orderDTO) throws Exception {

        String redisCode = (String) redisTemplate.opsForValue().get("validateCode");
        //判断验证码是否正确
        boolean isValidateCode = redisCode.equals(orderDTO.getValidateCode());
        log.info("判断验证码是否正确:{}", isValidateCode);
        //字符串转换为日期
        Date orderDate = DateUtils.parseString2Date(orderDTO.getOrderDate());
        //根据预约日期查询预约设置
        OrderSetting orderSetting = orderMapper.findByOrderDate(orderDate);
        //判断预约设置人数是否非空
        boolean isNotNullNumber = orderSetting != null;
        log.info("判断预约设置人数是否非空:{}", isNotNullNumber);

        //判断预约设置人数是否大于已经预约人数
        boolean isnotfull = false;
        if (isNotNullNumber) {
            isnotfull = orderSetting.getNumber() > orderSetting.getReservations();
        }
        log.info("判断预约设置人数是否大于已经预约人数:{}", isnotfull);

        Order order = orderMapper.findOrderByOrderDate(orderDTO.getIdCard(), orderDate, orderDTO.getSetmealId());
        //查询当前用户是否未预约
        boolean isNotNullOrder = order == null;
        log.info("查询当前用户是否未预约:{}", isNotNullOrder);

        //判断用户能否预约
        if (isValidateCode && isNotNullNumber && isnotfull && isNotNullOrder) {
            //判断当前用户是否为会员,如果不是会员，则添加会员
            Member member = orderMapper.findMemberByIdCard(orderDTO.getIdCard());
            Member member1 = new Member();
            member1.setIdCard(orderDTO.getIdCard());
            member1.setName(orderDTO.getName());
            member1.setSex(orderDTO.getSex());
            member1.setPhoneNumber(orderDTO.getTelephone());
            if (member == null) {
                orderMapper.addMember(member1);
            }
            //添加预约信息
            member1.setId(orderMapper.findMemberByIdCard(orderDTO.getIdCard()).getId());
            orderMapper.submitOrder(orderDate, member1.getId(),orderDTO.getSetmealId());
            //预约人数加1
            orderSetting.setReservations(orderSetting.getReservations() + 1);
            orderMapper.editNumberByOrderDate(orderSetting);
            redisTemplate.delete("validateCode");
        }
    }

    /**
     * 发送验证码
     *
     * @param telephone
     */
    @Override
    public void sendValidateCode4Order(String telephone) {
        //生成验证码
        String validateCode = String.valueOf((int) ((Math.random() + 1) * 100000));
        //发送验证码
//        SMSUtils.sendMessage("瑞吉外卖","SMS_207860000",telephone,validateCode);
        //将验证码存到Redis中
        redisTemplate.opsForValue().set("validateCode", validateCode);
        log.info("验证码：{}", validateCode);
        //定时删除验证码5分钟
        redisTemplate.expire("validateCode", 5, java.util.concurrent.TimeUnit.MINUTES);
    }
}
